package com.henu.zmb;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

import com.henu.R;
import com.henu.zmb.DBhelper.DBhelper;
import com.henu.zmb.adapter.LoseGoods_adapter;
import com.henu.zmb.entity.LoseGoods;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class UserLoseGoods_MainActivity extends AppCompatActivity {
    ListView lv ;
    LoseGoods_adapter lg_adapter;
    ArrayList<LoseGoods> list = new ArrayList<LoseGoods>();
    ImageButton gulgm_return;
    Button aulgm_addlosegoods;
    protected void showLoseGoods() {
        list.clear();
        // 写入数据
        Bmob.initialize(this, "a24db0fac16b375a97072b85080ea20f");
        // 写入类别数据
        BmobQuery<LoseGoods> query = new BmobQuery<LoseGoods>();
        //返回50条数据，如果不加上这条语句，默认返回10条数据
        query.addWhereEqualTo("UserId", new BmobPointer(BmobUser.getCurrentUser()));
        query.setLimit(50);
        //执行查询方法
        query.findObjects(new FindListener<LoseGoods>() {
            @Override
            public void done(List<LoseGoods> object, BmobException e) {
                if(e==null){
                    list =(ArrayList)object;
                    lg_adapter.addAllData(list); // 绑定数据
                    lg_adapter.notifyDataSetChanged();// 显示
                }else{
                    System.out.println("bmob失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userlosegoods_main);
        gulgm_return = (ImageButton)findViewById(R.id.aulgm_return);
        lv = (ListView)findViewById(R.id.aulgm_lvlosegoods);// 获取控件
        aulgm_addlosegoods = (Button)findViewById(R.id.aulgm_addlosegoods);
        lg_adapter = new LoseGoods_adapter(this,true); // 初始化适配器
        lv.setAdapter(lg_adapter);// 绑定适配器
        showLoseGoods();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(UserLoseGoods_MainActivity.this,LoseGoodsDetails_Activity.class); // 设置跳转的路径
                LoseGoods losegood = list.get(position); // 取出内容
                losegood.setUserId(BmobUser.getCurrentUser());
                intent.putExtra("losegood", losegood); // 需要实例化Goods
                startActivity(intent); // 跳转
            }
        });
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(UserLoseGoods_MainActivity.this);
                dialog.setTitle("是否删除");
                dialog.setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DBhelper<LoseGoods> db = new DBhelper<LoseGoods>();
                        db.Delete(list.get(position));
                        showLoseGoods();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.create();
                dialog.show();
                return false;
            }
        });
        aulgm_addlosegoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UserLoseGoods_MainActivity.this,AddLoseGoods_Activity.class); // 设置跳转的路径
                startActivity(intent); // 跳转
            }
        });
        gulgm_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
